home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6448 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  48 lines

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual Function in private part?
  5. Date: 8 Feb 1996 14:17:52 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4fd0mg$hf3@dawn.mmm.com>
  8. References: <4f7s5g$ntt@mcmail.CIS.McMaster.CA>
  9. Reply-To: kjhopps@mmm.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Hong Shen (g9326161@mcmail.cis.McMaster.CA) wrote:
  13. > Is there any reasons to make a member function in the private section of
  14. > a class virtual?
  15.  
  16. The decisions about whether to make a function private or virtual are
  17. orthogonal.  You make a function private if you want no derived classes
  18. to call it.  You make a function virtual if you want to allow derived
  19. classes to override it.
  20.  
  21. This may seem like a contradiction, but consider this example:
  22.  
  23. class Mutex
  24. {
  25. public:
  26.     ...
  27.     void lock() { doSomeStuff(); do_lock(); doSomeOtherStuff(); }
  28.     ...
  29. private:
  30.     void doSomeStuff();
  31.     void doSomeOtherStuff();
  32.     virtual void do_lock() = 0;
  33. };
  34.  
  35. In this example, the intent is that a derived class must provide the
  36. do_lock() function, but that the only way to call it is through the
  37. lock() function.  Of course there is no way to prevent the derived
  38. class from calling do_lock() directly, but at least further derivatives
  39. will be unable to call it.
  40. --
  41. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  42. 3M Company                      phone:  (612) 737-4643
  43. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  44. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  45.     But 3M speaks for me -- I did not write the following line:
  46.  
  47. Opinions expressed herein are my own and may not represent those of 3M.
  48.